package com.mangguo.tingjian.content.service.impl;

import com.mangguo.tingjian.content.dto.UvpvCountResult;
import com.mangguo.tingjian.content.entity.CategoryLayout;
import com.mangguo.tingjian.content.entity.DwClick;
import com.mangguo.tingjian.content.mapper.CategoryLayoutMapper;
import com.mangguo.tingjian.content.mapper.DwClickMapper;
import com.mangguo.tingjian.content.service.IDwClickService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * click同步表 服务实现类
 * </p>
 *
 * @author auto
 * @since 2019-11-27
 */
@Service
@AllArgsConstructor
public class DwClickServiceImpl extends ServiceImpl<DwClickMapper, DwClick> implements IDwClickService {

    private final DwClickMapper dwClickMapper;

    private final CategoryLayoutMapper categoryLayoutMapper;

    @Override
    public List<UvpvCountResult> topUvByLvlBetweenDate(Integer start, Integer end, Long lvl1, Long lvl2) {
        List<UvpvCountResult> list = dwClickMapper.topUvByLvlBetweenDate(start, end, lvl1, lvl2);
        List<Long> lvl3List = new ArrayList<>();
        list.forEach(uvpvCountResult -> lvl3List.add(uvpvCountResult.getLvl3()));
        Map<Long, Long>  countMap = new HashMap<>();
        dwClickMapper.countPvByLvl3sBetweenDate(start, end, lvl1, lvl2, lvl3List).forEach(uvpvCountResult -> {
            countMap.put(uvpvCountResult.getLvl3(), uvpvCountResult.getPvCount());
        });

        list.forEach(e -> {
            CategoryLayout categoryLayout = categoryLayoutMapper.selectById(e.getLvl3());
            if(categoryLayout != null){
                e.setTitle(categoryLayout.getTitle());
                e.setTemplateType(categoryLayout.getTemplateType());
            }
            e.setPvCount(countMap.get(e.getLvl3()));
        });
        return list;
    }

    @Override
    public List<UvpvCountResult> topPvByLvlBetweenDate(Integer start, Integer end, Long lvl1, Long lvl2) {
        List<UvpvCountResult> list = dwClickMapper.topPvByLvlBetweenDate(start, end, lvl1, lvl2);
        list.forEach(e -> {
            CategoryLayout categoryLayout = categoryLayoutMapper.selectById(e.getLvl3());
            e.setTitle(categoryLayout.getTitle());
            e.setTemplateType(categoryLayout.getTemplateType());
            e.setUvCount(dwClickMapper.countUvByLvl3BetweenDate(start, end, lvl1, lvl2, e.getLvl3()));
        });
        return list;
    }
}
